<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="../../assets/img/login_icon.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        :inline="false"
        size="normal"
        class="form"
        center
      >
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                v-model="form.code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="7"
              ><img
                :src="imgUrl"
                alt=""
                width="100%"
                height="40px"
                style="margin-left:15px"
                @click="changCaptcha"
            /></el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="checked">
          <el-checkbox
            v-model="form.checked"
            style="margin-right:10px"
          ></el-checkbox
          >我已阅读并同意<el-link type="primary">用户协议</el-link>和<el-link
            type="primary"
            >隐私条款</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="default"
            style="width:100%"
            @click="login"
            >登 录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="default"
            style="width:100%"
            @click="$refs.register.dialogVisible = true"
            >注 册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="../../assets/img/login_bg.png" alt="" />
    </div>
    <register ref="register"></register>
  </div>
</template>
<script>
// 导入子组件
import register from './register'
// 导入token的文件
import { saveToken } from '../../utils/token'
export default {
  name: 'login',
  // 注册子组件
  components: {
    register
  },
  data () {
    return {
      imgUrl: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      form: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: true /* 发送请求传递参数时，参数多了没事，但是不能少了 */
      },
      rules: {
        phone: [
          {
            required: true,
            validator: (rule, value, callback) => {
              // 1.非空判断
              if (!value) return callback(new Error('手机号不能为空'))
              // 2.定义手机号的正则
              const reg = /^1[3-9][0-9]{9}$/
              // 3.判断用户输入的内容是否符合
              if (!reg.test(value)) return callback(new Error('手机号不合法'))
              // 4.符合也要调用回调函数
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 8, max: 16, message: '密码必须在8-16位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 4, max: 4, message: '验证码必须是4位', trigger: 'blur' }
        ],
        checked: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) return callback(new Error('用户协议必须勾选'))
              callback()
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 验证码图片被点击切换验证码
    changCaptcha () {
      this.imgUrl =
        process.env.VUE_APP_BASEURL + '/captcha?type=login&t=' + Date.now()
    },
    // 登录操作
    login () {
      // 1.全局验证表单
      this.$refs.form.validate(async valid => {
        // 2.判断验证是否通过
        if (!valid) return
        // 3.验证通过根据表单内容发送请求登录
        const res = await this.$axios.post('/login', this.form)
        // 4.判断是否登录成功
        if (res.code === 200) {
          // 5.提示用户登录成功
          this.$message.success('登录成功~')
          // 6.保存token
          // localStorage.setItem('heimamm662', res.data.token)
          saveToken(res.data.token)
          // 7.跳转到首页
          this.$router.push('layout')
        } else {
          // 8.登录失败也提示用户
          this.$message.error(res.message)
          // 9.刷新验证码
          this.changCaptcha()
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .form {
      margin-top: 29px;
      .captcha {
        width: 100%;
        height: 40px;
        margin-left: 10px;
      }
    }
  }
}
</style>
